// 获取指定时间 后的 addDay 天
export function getNextDay(data, addDay, temStr = 'yyyyMMdd') {
  if (data.indexOf('-') == -1) {
    data = data.replace(/^(\d{4})(\d{2})(\d{2})/, '$1-$2-$3')
  }
  var date = new Date(data) // 标准事件 Thu Jun 02 2022 17:26:06 GMT+0800 (中国标准时间)
  date.setDate(date.getDate() + Number(addDay))
  // date.setDate(date.getDate()) // 时间戳 1654161966100
  let year = date.getFullYear()
  let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  if (temStr === 'yyyy') {
    return String(year)
  } else if (temStr === 'yyyyMM') {
    return String(year) + String(month)
  } else if (temStr === 'yyyyMMdd') {
    return String(year) + String(month) + String(day)
  } else if (temStr === 'yyyy-MM-dd') {
    return String(year) + '-' + String(month) + '-' + String(day)
  } else if (temStr === 'yyyy-MM-dd HH:mm:ss') {
    return String(year) + '-' + String(month) + '-' + String(day) + ' ' + String(hour) + ':' + String(minute) + ':' + String(second)
  }
}

// 单位转换，亿，万，保留两位小数
export function changeUnit(value = '0', unit = '') {
  let returnObj = {
    value: '',
    unit: '',
  }
  value = String(value)
  if (value.split('.')[0].length >= 9) {
    value = (parseFloat(value) / 100000000).toFixed(2)
    unit = '亿' + unit
  } else if (value.split('.')[0].length >= 5) {
    value = (parseFloat(value) / 10000).toFixed(2)
    unit = '万' + unit
  } else if (value.split('.').length > 1) {
    value = parseFloat(value).toFixed(2)
  }
  returnObj.value = value
  returnObj.unit = unit
  return returnObj.value + ' ' + returnObj.unit
}

// 数组 对象 的深拷贝
export function deepClone(source) {
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach((keys) => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = source[keys].constructor === Array ? [] : {}
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

// 获取 96 个时间点
export function get96TimePoint() {
  let xData = []
  for (let i = 0; i < 96; i++) {
    xData.push(`${Math.floor(i / 4).toString().padStart(2, '0')}:${(i % 4 * 15).toString().padStart(2, '0')}`)
  }
  return xData
}

// 随机获取一个颜色
export function randColor() {
  var r = Math.floor(Math.random() * 256)
  var g = Math.floor(Math.random() * 256)
  var b = Math.floor(Math.random() * 256)
  return '#' + r.toString(16).padStart(2, '0') + g.toString(16).padStart(2, '0') + b.toString(16).padStart(2, '0')
}

// 处理日期 转换成 时间戳
export function timeChangeStr(time) {
  var date = new Date(time)
  return date.getTime(date)
}

// 处理日期 转换成 20220331
export function handleDateNo(time) {
  let fillZero = (n) => {
    let result = (n).toString().length === 1 ? ('0' + n) : n
    return result
  }
  var myDate = new Date(time)
  let year = myDate.getFullYear()
  let month = myDate.getMonth() + 1
  let date = myDate.getDate()
  let hour = myDate.getHours()
  let minute = myDate.getMinutes()
  let second = myDate.getSeconds()
  let result = `${year}${fillZero(month)}${fillZero(date)}`
  return result
}

// 处理日期 转换成 2022-03-31 11:12:54
export function handleDateHave(time) {
  let fillZero = (n) => {
    let result = (n).toString().length === 1 ? ('0' + n) : n
    return result
  }
  console.log(time)
  var myDate = new Date(time)
  console.log(myDate)
  let year = myDate.getFullYear()
  let month = myDate.getMonth() + 1
  let date = myDate.getDate()
  let hour = myDate.getHours()
  let minute = myDate.getMinutes()
  let second = myDate.getSeconds()
  let result = `${year}-${fillZero(month)}-${fillZero(date)} ${fillZero(hour)}:${fillZero(minute)}:${fillZero(second)}`
  return result
}

// 处理数字 数字/小数
export function renderNumber(num = '0', size = [16, 14]) {
  let splitNum = (num + '').split('.')
  if (splitNum.length == 1) {
    return `<span style="font-size: ${size[0]}px;">${splitNum[0]}</span>`
  } else {
    return `<span style="font-size: ${size[0]}px;">${splitNum[0]}</span>.<span style="font-size: ${size[1]}px;">${splitNum[1]}</span>`
  }
}

// 扁平数据转换成 tree 结构 方法
export function buidTree(data, root) {
  let tree = []
  let temp = {}
  data = data.sort((a, b) => Number(a.id) - Number(b.id))
  for (let d of data) {
    let id = d.id;
    let parentId = d.parentId
    if (!temp[id]) {
      temp[id] = { text: d.text, value: d.id }
      temp[id].children = []
    }
    if (parentId == root) {
      tree.push(temp[id])
    } else {
      temp[parentId].children.push(temp[id])
    }
  }
  return tree
}